// 设置中考时间，格式为：年，月 - 1，日，时，分，秒
var targetDate = new Date(2025, 6 - 1, 21, 0, 0, 0);

function updateCountdown() {
  var now = new Date();
  var difference = targetDate - now;

  if (difference <= 0) {
    document.getElementById("countdown").innerHTML = "中考已开始！";
  } else {
    var days = Math.floor(difference / (1000 * 60 * 60 * 24));
    var hours = Math.floor(
      (difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
    );
    var minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((difference % (1000 * 60)) / 1000);

    document.getElementById(
      "countdown"
    ).innerHTML = `${days}天${hours}时${minutes}分${seconds}秒`;
  }
}

function updateTime() {
  const now = new Date();
  const hours = now.getHours().toString().padStart(2, "0");
  const minutes = now.getMinutes().toString().padStart(2, "0");
  const seconds = now.getSeconds().toString().padStart(2, "0");
  document.getElementById("hours").textContent = hours;
  document.getElementById("minutes").textContent = minutes;
  document.getElementById("seconds").textContent = seconds;
}

// 性能优化的粒子动画
const canvas = document.createElement("canvas");
canvas.id = "particles";
document.body.appendChild(canvas);
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.position = "fixed";
canvas.style.top = "0";
canvas.style.left = "0";
canvas.style.zIndex = "0";
canvas.style.pointerEvents = "none";

// 减少粒子数量以提高性能
const particles = [];
const colors = [
  "rgba(67, 97, 238, 0.6)",
  "rgba(58, 12, 163, 0.5)",
  "rgba(247, 37, 133, 0.5)",
  "rgba(255, 255, 255, 0.8)",
];

// 使用对象池模式优化粒子创建
function createParticle() {
  return {
    x: Math.random() * canvas.width,
    y: Math.random() * canvas.height,
    size: Math.random() * 3 + 2,
    color: colors[Math.floor(Math.random() * colors.length)],
    speed: Math.random() * 2 + 1,
    velocity: Math.random() * 0.5 - 0.25,
    opacity: Math.random() * 0.6 + 0.4,
  };
}

// 初始化粒子（数量减少到50个）
for (let i = 0; i < 50; i++) {
  particles.push(createParticle());
}

// 使用requestAnimationFrame的节流版本
let lastTime = 0;
const fps = 30;
const frameInterval = 1000 / fps;

function animateParticles(timestamp) {
  if (timestamp - lastTime > frameInterval) {
    lastTime = timestamp;

    // 使用clearRect替代渐变覆盖以提高性能
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // 绘制半透明背景创造拖尾效果
    ctx.fillStyle = "rgba(240, 244, 255, 0.2)";
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    particles.forEach((particle) => {
      particle.y += particle.speed;
      particle.x += particle.velocity;

      if (particle.y > canvas.height + particle.size) {
        Object.assign(particle, createParticle());
        particle.y = -particle.size;
      }

      // 简化绘制逻辑
      ctx.beginPath();
      ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
      ctx.fillStyle = particle.color;
      ctx.globalAlpha = particle.opacity;
      ctx.fill();
    });
  }

  requestAnimationFrame(animateParticles);
}

// 优化resize事件处理
let resizeTimeout;
window.addEventListener("resize", () => {
  clearTimeout(resizeTimeout);
  resizeTimeout = setTimeout(() => {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
  }, 200);
});

document.addEventListener("DOMContentLoaded", function () {
  updateCountdown();
  updateTime();
  setInterval(updateCountdown, 1000);
  setInterval(updateTime, 1000);

  // 启动优化后的粒子动画
  requestAnimationFrame(animateParticles);

  // 随机生成流星
  function createMeteor() {
    const meteor = document.createElement("div");
    meteor.className = "meteor";
    meteor.style.top = Math.random() * 50 + "%";
    meteor.style.left = Math.random() * 50 + "%";
    meteor.style.animationDelay = Math.random() * 3 + "s";
    document.body.appendChild(meteor);

    // 移除流星元素
    setTimeout(() => {
      meteor.remove();
    }, 3000);
  }

  // 每3秒生成一个流星
  setInterval(createMeteor, 3000);
  createMeteor();
});
